<?php

namespace backend\controllers;

use Yii;
use backend\models\search\MissionSearch;
use backend\models\Mission;
use backend\actions\CreateAction;
use backend\actions\UpdateAction;
use backend\actions\IndexAction;
use backend\actions\DeleteAction;
use backend\actions\SortAction;
use backend\actions\ViewAction;
use common\models\User;
use yii\data\ActiveDataProvider;
/**
 * MissionController implements the CRUD actions for Mission model.
 */
class MissionController extends \yii\web\Controller
{
    public function actions()
    {
        return [
            'index'      => [
                'class' => IndexAction::className(),
                'data'  => function () {

                    $searchModel  = new MissionSearch();
                    $dataProvider = $searchModel->search(yii::$app->getRequest()->getQueryParams());

                    return [
                        'dataProvider' => $dataProvider,
                        'searchModel'  => $searchModel,
                    ];

                }
            ],
            'create'     => [
                'class'      => CreateAction::className(),
                'modelClass' => Mission::className(),
            ],
            'update'     => [
                'class'      => UpdateAction::className(),
                'modelClass' => Mission::className(),
            ],
            'delete'     => [
                'class'      => DeleteAction::className(),
                'modelClass' => Mission::className(),
            ],
            'sort'       => [
                'class'      => SortAction::className(),
                'modelClass' => Mission::className(),
            ],
            'view-layer' => [
                'class'      => ViewAction::className(),
                'modelClass' => Mission::className(),
            ],
        ];
    }

    public function actionUserList($id)
    {
//        $query = User::find()->select(['status'=>'user.status','j_status'=>'job_record.status']);
        $query = User::find()->select(['user.*', 'job_record.jid']);
        $query->joinWith('records');
        $dateProvider = new ActiveDataProvider([
            'query' => $query,
            'sort'  => [
                'defaultOrder' => [
                    'id' => SORT_ASC
                ]
            ]
        ]);

        $query->where([
            'job_record.jid' => $id,
            'user.status'    => User::STATUS_ACTIVE,
        ]);


//        var_dump($dateProvider->query);
        return $this->render('user-list', ['dataProvider' => $dateProvider]);

    }

}
